package common.utils;

import org.apache.commons.lang.StringUtils;

public class PageUtil {
	
	/**
	 *
	 * @param everyPage  每页显示数
	 * @param totalCount  总记录数
	 * @param currentPage  当前页数
	 * @return TODO
	 */
	public static Page createPage(int everyPage, int totalCount, int currentPage) {
		everyPage = getEveryPage(everyPage);
		int totalPage = getTotalPage(everyPage, totalCount);
		currentPage = getCurrentPage(currentPage,totalPage);
		int beginIndex = getBeginIndex(everyPage, currentPage);
		boolean hasPrePage = getHasPrePage(currentPage);
		boolean hasNextPage = getHasNextPage(totalPage, currentPage);
		return new Page(everyPage, totalCount, totalPage, currentPage,
				beginIndex, hasPrePage, hasNextPage);
	}

	private static int getEveryPage(int everyPage) {
		return everyPage == 0 ? 10 : everyPage;
	}

	private static int getCurrentPage(int currentPage,int totalPage) {
		return currentPage <1||currentPage>totalPage ? 1 : currentPage;
	}

	private static int getTotalPage(int everyPage, int totalCount) {
		return totalCount==0?0:(totalCount-1)/everyPage+1;
	}

	private static int getBeginIndex(int everyPage, int currentPage) {
		return (currentPage - 1) * everyPage;
	}

	private static boolean getHasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}

	private static boolean getHasNextPage(int totalPage, int currentPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}
	public static int getCurrent(String currentTmp) {
		return StringUtils.isBlank(currentTmp) ? 1 : Integer.valueOf(currentTmp);
	}
}
